package listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import cjgl.dao.CounterDAO;

/**
 * Application Lifecycle Listener implementation class CounterListener
 *
 */
@WebListener
public class CounterListener implements ServletContextListener {
     private CounterDAO counterDAO=new CounterDAO();
    /**
     * Default constructor. 
     */
    public CounterListener() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent sce)  { 
    	  ServletContext ctxt=sce.getServletContext();
          Integer count=(Integer)ctxt.getAttribute("count");
          counterDAO.setValue(count);
    }

	/**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent sce)  { 
         Integer counter=counterDAO.getValue();
         ServletContext ctxt=sce.getServletContext();
         ctxt.setAttribute("counter", counter);
    }
	
}
